package com.meixiang.view.photoSelectActivity.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.activity.moments.ContentEditingActivity;
import com.meixiang.main.BaseActivity;
import com.meixiang.main.MXApplication;
import com.meixiang.util.AbStrUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.photoSelectActivity.adapter.PhotoShowFileAdapter;
import com.meixiang.view.photoSelectActivity.util.AlbumHelper;
import com.meixiang.view.photoSelectActivity.util.Bimp;
import com.meixiang.view.photoSelectActivity.util.ImageItem;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 作者：黑哥 on 2016/5/14 10:44
 * 用于显示相册文件
 */
public class PhotoShowFileActivity extends BaseActivity {
    @Bind(R.id.tv_back)
    TextView mtTvBack;
    @Bind(R.id.rl_title)
    RelativeLayout mRlTitle;
    @Bind(R.id.rv_recycler)
    RecyclerView mRecycler;
    @Bind(R.id.tv_cancel)
    TextView mTvCancel;
    @Bind(R.id.tv_confirm)
    TextView mTvConfirm;
    @Bind(R.id.tl_tab)
    RelativeLayout mTlTab;
    @Bind(R.id.tv_title)
    TextView mTvTitle;
    private Activity mActivity;
    private PhotoShowFileAdapter mAdapter;
    public static ArrayList<ImageItem> dataList;
    private AlbumHelper helper;
    private String type;
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_photo_show_file);
        mActivity = PhotoShowFileActivity.this;
        MXApplication.mApp.AddActivity(mActivity);
        ButterKnife.bind(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initTitle() {
        mtTvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initView() {
        type=getIntent().getStringExtra("photoType");
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());
        String folderName = getIntent().getStringExtra("folderName");

        if (getIntent().getParcelableArrayListExtra("list") != null) {
            dataList = getIntent().getParcelableArrayListExtra("list");
        }
//        public String imageId;
//        public String thumbnailPath;
//        public String imagePath;
//        private Bitmap bitmap;
//        public boolean isSelected = false;

        mTvTitle.setText(folderName);
        mAdapter = new PhotoShowFileAdapter(context, dataList,Bimp.tempSelectBitmap);
        mRecycler.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        mRecycler.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener(new PhotoShowFileAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RelativeLayout cbIntegral, int position, boolean isChecked) {
                if (isChecked) {
                    for(int i=0;i<Bimp.tempSelectBitmap.size();i++){
                        if(dataList.get(position).getImagePath().equals(Bimp.tempSelectBitmap.get(i).getImagePath())){
                            return;
                        }
                    }
                    Bimp.tempSelectBitmap.add(dataList.get(position));
                    AbToastUtil.showToast(mActivity, "(" +Bimp.tempSelectBitmap.size() + "/8" + ")");
                } else {

                    for(int i=0;i<Bimp.tempSelectBitmap.size();i++){
                        if(dataList.get(position).getImagePath().equals(Bimp.tempSelectBitmap.get(i).getImagePath())){
                            Bimp.tempSelectBitmap.remove(i);
                        }
                    }
                    AbToastUtil.showToast(mActivity, "(" + Bimp.tempSelectBitmap.size() + "/8" + ")");
                }
            }
        });
        mTvConfirm.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    @Override
    protected void setData() {

    }

    @Override
    public void onClick(View v) {
        if (v.equals(mTvConfirm)) {
            Intent intent=new Intent();
            if(!AbStrUtil.isEmpty(type)){
                intent.setClass(this,PhotoFileActivity.class);
                intent.putExtra("position",getIntent().getIntExtra("position",0));
                setResult(200,intent);
            }else{
                intent.setClass(mActivity, ContentEditingActivity.class);
                startActivity(intent);

            }
            finish();

        }else if(v.equals(mTvCancel)){
//            Bimp.tempSelectBitmap.clear();
//            Bimp.tempSelectBitmap.removeAll(tempDataList);
            PhotoShowFileActivity.this.finish();
        }

    }
}
